package com.bxd.baodan.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bxd.baodan.R;
import com.bxd.baodan.util.LogUtil;
import com.bxd.baodan.util.StringUtil;
import com.bxd.baodan.widget.MyTextWatcher;

/**
 *
 */

public class BaoxianFragment extends BaseStepFragment {
    RadioGroup socialInsure;
    RadioGroup otherInsure;
    LinearLayout other_shebao_ly;
    RadioButton have_social, no_social, have_other, no_other;

    private LinearLayout lifeLy;
    private LinearLayout seriousLy;
    private LinearLayout accidentLy;
    private LinearLayout medicalLy;
    private LinearLayout pensionLy;
    private LinearLayout educationLy;
/*
*
*  public String have_social_insurance;  //有社保
    public String have_business_insurance;//是否有其他商保*/


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        LogUtil.e("BaoxianFragment: ----CreateView");
        return inflater.inflate(R.layout.step_4_insure, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        title.setText(roleText + "的保障情况");
        String age = getDesignStep().age;
        int age_ = Integer.parseInt(age);
        if (age_ < 18) {
            title1.setText("您的少儿医保");
        } else {
            title1.setText(roleText + "的社会保险");
        }

        title2.setText(roleText + "的其它保险");


        socialInsure = (RadioGroup) view.findViewById(R.id.social_insure);
        otherInsure = (RadioGroup) view.findViewById(R.id.other_insure);
        other_shebao_ly = (LinearLayout) view.findViewById(R.id.other_shebao_ly);
        have_social = (RadioButton) view.findViewById(R.id.have_social);
        no_social = (RadioButton) view.findViewById(R.id.no_social);
        have_other = (RadioButton) view.findViewById(R.id.have_other);
        no_other = (RadioButton) view.findViewById(R.id.no_other);


        lifeLy = (LinearLayout) view.findViewById(R.id.life_ly);
        seriousLy = (LinearLayout) view.findViewById(R.id.serious_ly);
        accidentLy = (LinearLayout) view.findViewById(R.id.accident_ly);
        medicalLy = (LinearLayout) view.findViewById(R.id.medical_ly);
        pensionLy = (LinearLayout) view.findViewById(R.id.pension_ly);
        educationLy = (LinearLayout) view.findViewById(R.id.education_ly);

        other_shebao_ly.setVisibility(View.GONE);
        String hs = getDesignStep().have_social_insurance;
        if (!StringUtil.isEmpty(hs)) {
            if (hs.equals("have")) {
                have_social.setChecked(true);
                no_social.setChecked(false);
            } else if (hs.equals("no")) {
                no_social.setChecked(true);
                have_social.setChecked(false);
            }
        }


        String[] aa = {getDesignStep().business_insurance_life,
                getDesignStep().business_insurance_serious,
                getDesignStep().business_insurance_accident,
                getDesignStep().business_insurance_medical,
                getDesignStep().business_insurance_pension,
                getDesignStep().business_insurance_education
        };


        EditText[] et = {
                getBusinessInsuranceLife(),
                getBusinessInsuranceSerious(),
                getBusinessInsuranceAccident(),
                getBusinessInsuranceMedical(),
                getBusinessInsurancePension(),
                getBusinessInsuranceEducation()
        };

        String a = getDesignStep().have_business_insurance;
        if (!StringUtil.isEmpty(a)) {
            if (a.equals("have")) {


                have_other.setChecked(true);
                no_other.setChecked(false);

                otherVisible();


                for (int i = 0; i < aa.length; i++) {
                    if (!StringUtil.isEmpty(aa[i])) {
                        et[i].setText(aa[i]);
                    }
                }

            } else if (a.equals("no")) {

                have_other.setChecked(false);
                no_other.setChecked(true);

                other_shebao_ly.setVisibility(View.GONE);
                getDesignStep().business_insurance_life = "";
                getDesignStep().business_insurance_serious = "";
                getDesignStep().business_insurance_accident = "";
                getDesignStep().business_insurance_medical = "";
                getDesignStep().business_insurance_pension = "";
                getDesignStep().business_insurance_education = "";
            }
        }

        socialInsure.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.have_social) {
                    getDesignStep().have_social_insurance = "have";

                } else if (checkedId == R.id.no_social) {
                    getDesignStep().have_social_insurance = "no";
                }

            }
        });

        otherInsure.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.have_other) {
                    getDesignStep().have_business_insurance = "have";
                    otherVisible();

                } else if (checkedId == R.id.no_other) {
                    getDesignStep().have_business_insurance = "no";
                    other_shebao_ly.setVisibility(View.GONE);
                    getDesignStep().business_insurance_life = "";
                    getDesignStep().business_insurance_serious = "";
                    getDesignStep().business_insurance_accident = "";
                    getDesignStep().business_insurance_medical = "";
                    getDesignStep().business_insurance_pension = "";
                    getDesignStep().business_insurance_education = "";

                }
            }
        });


        getBusinessInsuranceLife().addTextChangedListener(new MyTextWatcher() {


            @Override
            public void afterTextChanged(Editable s) {
                getDesignStep().business_insurance_life = getBusinessInsuranceLife().getText().toString().trim();
            }
        });


        getBusinessInsuranceSerious().addTextChangedListener(new MyTextWatcher() {


            @Override
            public void afterTextChanged(Editable s) {
                getDesignStep().business_insurance_serious = getBusinessInsuranceSerious().getText().toString().trim();
            }
        });


        getBusinessInsuranceAccident().addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                getDesignStep().business_insurance_accident = getBusinessInsuranceAccident().getText().toString().trim();
            }
        });


        getBusinessInsuranceMedical().addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                getDesignStep().business_insurance_medical = getBusinessInsuranceMedical().getText().toString().trim();
            }
        });


        getBusinessInsurancePension().addTextChangedListener(new MyTextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                getDesignStep().business_insurance_pension = getBusinessInsurancePension().getText().toString().trim();
            }
        });

        getBusinessInsuranceEducation().addTextChangedListener(new MyTextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                getDesignStep().business_insurance_education = getBusinessInsuranceEducation().getText().toString().trim();
            }
        });
    }

    private void otherVisible() {
        other_shebao_ly.setVisibility(View.VISIBLE);
        int age_ = Integer.parseInt(getDesignStep().age);

        if (age_ < 18) {
            lifeLy.setVisibility(View.GONE);
            pensionLy.setVisibility(View.GONE);

        } else if (age_ > 17 && age_ < 36) {
            lifeLy.setVisibility(View.GONE);
            pensionLy.setVisibility(View.GONE);
            educationLy.setVisibility(View.GONE);


        } else if (age_ > 36) {
            educationLy.setVisibility(View.GONE);
        }
    }


    private EditText getBusinessInsuranceLife() {
        return (EditText) getView().findViewById(R.id.business_insurance_life);
    }

    private EditText getBusinessInsuranceSerious() {
        return (EditText) getView().findViewById(R.id.business_insurance_serious);
    }

    private EditText getBusinessInsuranceAccident() {
        return (EditText) getView().findViewById(R.id.business_insurance_accident);
    }

    private EditText getBusinessInsuranceMedical() {
        return (EditText) getView().findViewById(R.id.business_insurance_medical);
    }

    private EditText getBusinessInsurancePension() {
        return (EditText) getView().findViewById(R.id.business_insurance_pension);
    }

    private EditText getBusinessInsuranceEducation() {
        return (EditText) getView().findViewById(R.id.business_insurance_education);
    }


    @Override
    public void onDestroyView() {
        LogUtil.e("BaoxianFragment------: onDestroyView");
        super.onDestroyView();
    }

}
